package com.JZ.QM.controller;

import com.JZ.QM.common.entity.CombinationDetailInfoEntity;
import com.JZ.QM.common.entity.UserCollectEntity;
import com.JZ.QM.common.utils.CommonResult;
import com.JZ.QM.dto.CollectAndCommentDto;
import com.JZ.QM.feign.UserInfoFeignService;
import com.JZ.QM.service.UserCollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author: cyc
 * @email cyc@gmail.com
 * @date: 2020/7/6 0:45
 */
@RestController
@RefreshScope
@Slf4j
@RequestMapping(value="/userCollect")
public class UserCollectController {

    @Resource
    UserCollectService userCollectService;
    @Resource
    UserInfoFeignService userInfoFeignService;

    /**
     * 用户对组合进行收藏
     * @param collectAndCommentDto 收藏数据传输对象
     * @return
     */
    @RequestMapping("/collect")
    public CommonResult collect(@Valid CollectAndCommentDto collectAndCommentDto){
        //用户基本信息
        LinkedHashMap userInfo=new LinkedHashMap();
        //用户收藏的更新状态
        boolean collectState=false;
        //显示消息信息内容
        String content="";
        //获取用户的基本信息
        System.out.println("用户id为"+collectAndCommentDto.getUserId());
        userInfo= (LinkedHashMap) userInfoFeignService.getUserInfo(collectAndCommentDto.getUserId()).get("userInfo");
        if(userInfo.isEmpty()){
            return CommonResult.error(201,"该用户不存在");
        } else {
            //数据库进行更新，并且返回更新状态
            collectState=userCollectService.collect(collectAndCommentDto);
            //设置消息显示内容
            if (collectAndCommentDto.getCollectState() ==1){
                content="收藏成功";
            } else {
                content="取消收藏成功";
            }
            if(collectState){
                return CommonResult.success(content);
            } else {
                return CommonResult.error(201,"数据库更新失败");
            }
        }

    }

    /**
     *获取用户的收藏列表
     * @param userId
     * @return
     */
    @RequestMapping("/UserCollectComList")
    public CommonResult getUserCollectComList(Long userId){
        //组合详细信息列表
        ArrayList<CombinationDetailInfoEntity> combinationDetailInfoEntities=new ArrayList<>();

        //获取用户收藏组合列表信息
        combinationDetailInfoEntities=userCollectService.getUserCollectComList(userId);
        if(combinationDetailInfoEntities.isEmpty()){
            return CommonResult.success().put("userCollectList",new ArrayList().toArray());
        } else {
            return CommonResult.success().put("userCollectList",combinationDetailInfoEntities);
        }
    }

    /**
     * 判断某一个组合是否已经收藏
     * @param userId 用户id
     * @param comBaseId zuhe id
     * @return
     */
    @RequestMapping("/comCollectState")
    public CommonResult comCollectState(Long userId,Long comBaseId){
        //用户组合收藏信息
        UserCollectEntity userCollectEntity=new UserCollectEntity();
        //组合的收藏状态
         Boolean collectSate=false;

        //查询是否存在组合
        collectSate=userCollectService.getComCollectState(userId,comBaseId);
        return CommonResult.success().put("comCollectState",collectSate);
    }



}
